/**
 * 
 */
package co.edu.uniandes.mdd.project1.sample.view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import co.edu.uniandes.mdd.project1.sample.domain.Enemy;
import co.edu.uniandes.mdd.project1.sample.domain.Item;
import co.edu.uniandes.mdd.project1.sample.domain.Player;

/**
 * @author Leonardo
 * 
 */
public class PanelBoard extends JPanel implements Observer {

	public static int imageSize = 50;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// private Ellipse2D playerImage;
	Graphics2D g2;
	private GameImage playerImage;
	private GameImage[] enemies;
	private GameImage[] pellets;
	private GameImage[] powerPellets;
	private GameImage[] passiveElements;

	public PanelBoard() {
		setBorder(new CompoundBorder(new EmptyBorder(0, 0, 5, 0),
				new TitledBorder("Laberinto")));
		//setSize(500, 300);
		setDoubleBuffered(true);
		// playerImage = new Ellipse2D.Double( 0, 0, 15, 15 );
		// try {
		// playerImage = new MyImage();
		// playerImage.setImage(ImageIO.read(new
		// File("C:\\MDD\\project1wspc\\Torque\\MyGame\\game\\data\\images\\pacman_2.PNG")));
		//
		// } catch (IOException ex) {
		// // handle exception...
		// }

		repaint();
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setColor(Color.black);
		drawPlayer();
	}

	private void drawPlayer() {
		// g2.draw( this.playerImage );
		if (pellets != null) {
			for (int i = 0; i < pellets.length; i++) {
				if (pellets[i].isVisible()) {
					g2.drawImage(this.pellets[i].getImage(),
							this.pellets[i].getX(), this.pellets[i].getY(), null);
				}
			}
		}
		if (powerPellets != null) {
			for (int i = 0; i < powerPellets.length; i++) {
				if (powerPellets[i].isVisible()) {
					g2.drawImage(this.powerPellets[i].getImage(),
							this.powerPellets[i].getX(), this.powerPellets[i].getY(), null);
				}
			}
		}
		if (playerImage.isVisible()) {
			g2.drawImage(playerImage.getImage(), playerImage.getX(),
					playerImage.getY(), null);
		}
		if (enemies != null) {
			for (int i = 0; i < enemies.length; i++) {
				if (enemies[i].isVisible()) {
					g2.drawImage(this.enemies[i].getImage(),
							this.enemies[i].getX(), this.enemies[i].getY(),
							null);
				}
			}
		}
		if (passiveElements != null) {
			for (int i = 0; i < passiveElements.length; i++) {
				if (passiveElements[i].isVisible()) {
					g2.drawImage(this.passiveElements[i].getImage(),
							this.passiveElements[i].getX(),
							this.passiveElements[i].getY(), null);
				}
			}
		}

		

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
	 */
	@Override
	public void update(Observable o, Object arg) {
		if (arg instanceof Player) {
			Player player = (Player) arg;
			playerImage.setX(player.getPositionX());
			playerImage.setY(player.getPositionY());
			playerImage.setVisible(player.isVisible());
		} else if (arg instanceof Enemy) {
			Enemy enemy = (Enemy) arg;
			if (enemies != null) {
				enemies[enemy.getPosition()].setX(enemy.getPositionX());
				enemies[enemy.getPosition()].setY(enemy.getPositionY());
				enemies[enemy.getPosition()].setVisible(enemy.isVisible());
			}
		}else if (arg instanceof Item) {
			Item item = (Item) arg;
			if (pellets != null) {
				pellets[item.getPosition()].setX(item.getPositionX());
				pellets[item.getPosition()].setY(item.getPositionY());
				pellets[item.getPosition()].setVisible(item.isVisible());
			}
		}
		repaint();
	}

	public void setPlayer(GameImage player) {
		this.playerImage = player;
	}

	public void setEnemies(GameImage[] enemies) {
		this.enemies = enemies;
	}

	public void setPassiveElements(GameImage[] passiveElements) {
		this.passiveElements = passiveElements;
	}

	public void setItems(GameImage[] items) {
		this.pellets = items;
	}

}
